
	// --------------------------------------------------------------------------------------------------
	inline unsigned char* ByteBuffer::getData() const {
		return this->m_aData;
	}

	// --------------------------------------------------------------------------------------------------
	inline unsigned char* ByteBuffer::getDataAtPos() const {
		return this->m_aData+ m_nPos;
	}

	// --------------------------------------------------------------------------------------------------
	inline unsigned char* ByteBuffer::getDataAtPos(int nPos) const {
		return this->m_aData+ nPos;
	}

	// --------------------------------------------------------------------------------------------------
	inline unsigned int ByteBuffer::getLen() const {
		return this->m_nLen;
	}

	// --------------------------------------------------------------------------------------------------
	inline unsigned int ByteBuffer::getLenFromPos() const {
		int nResult = this->m_nLen - this->m_nPos;
		if ( nResult < 0 )
			nResult = 0;
		return nResult;
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::setLen(unsigned int nNewLen) {
		m_nLen = nNewLen;
		if(m_nLen > m_nCapacity)
			setCapacity ( m_nLen );
	}

	// --------------------------------------------------------------------------------------------------
	inline unsigned int ByteBuffer::getCapacity() const {
		return this->m_nCapacity;
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::grow(unsigned int nNewCapacity) {
		if( m_nMaxCapacity > 0 && nNewCapacity > m_nMaxCapacity )
			throw SystemException(
					boost::format("Unable to grow ByteBuffer(%1%). A new capacity can't be greater than the specified max capacity") %this
			);
		unsigned char* aNewBuffer = new unsigned char[nNewCapacity];
		memcpy(aNewBuffer, m_aData, m_nCapacity );
		disposeCurrent();
		m_aData = aNewBuffer;
		m_nCapacity = nNewCapacity;
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::setCapacity(unsigned int nCapacity) {
		if(m_nCapacity >= nCapacity) {
			m_nCapacity = nCapacity;
		}
		else
			grow(nCapacity);
	}

	// --------------------------------------------------------------------------------------------------
	inline unsigned int ByteBuffer::getAvailableCapacity() const {
		return m_nCapacity - m_nLen;
	}

	// --------------------------------------------------------------------------------------------------
	inline unsigned int ByteBuffer::getMaxCapacity() const {
		return m_nMaxCapacity;
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::setMaxCapacity(unsigned int nCapacity) {
		m_nMaxCapacity = nCapacity;
	}

	// --------------------------------------------------------------------------------------------------
	inline unsigned int ByteBuffer::getPos() const {
		return m_nPos;
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::setPos(unsigned int nNewPos) {
		m_nPos = nNewPos;
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::seek ( int nStep ) {
		if( m_nPos + nStep > m_nLen )
			throw SystemException(
					boost::format("ByteBuffer(%1%) seek overflow. Can't change the position of buffer to value greather than len") %this
			);
		m_nPos += nStep;
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::disposeCurrent() {
		if(m_bAutoDisposeFlag) {
			delete[] m_aData;
			m_aData = NULL;
		}
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::setup(
					unsigned char* aBuffer,
					unsigned int nLen,
					bool bCopingMode,
					bool bAutoDispose) {
		disposeCurrent();
		if ( bCopingMode ) {
			unsigned char* aNewBuffer = new unsigned char[nLen];
			memcpy(aNewBuffer,aBuffer, nLen);
			bAutoDispose = true;
			this->m_aData = aNewBuffer;
		}
		else {
			this->m_aData = aBuffer;
		}
		this->m_bAutoDisposeFlag = bAutoDispose;
		this->m_nCapacity = nLen;
		this->m_nLen = nLen;
		m_nPos = 0;
		m_nMarkedLen = 0;
		m_nMarkedPos = 0;
	}

	// --------------------------------------------------------------------------------------------------
	inline ByteBuffer::ByteBuffer() :
			m_nLen(0),
			m_nCapacity(0),
			m_nMaxCapacity(0),
			m_aData(NULL),
			m_bAutoDisposeFlag(true),
			m_nPos(0),
			m_nMarkedLen(0),
			m_nMarkedPos(0) {
	}

	// --------------------------------------------------------------------------------------------------
	inline ByteBuffer::ByteBuffer(unsigned int nCapacity) :
			m_nLen(0),
			m_nCapacity(0),
			m_nMaxCapacity(0),
			m_aData(NULL),
			m_bAutoDisposeFlag(false),
			m_nPos(0),
			m_nMarkedLen(0),
			m_nMarkedPos(0)  {
		unsigned char* aNewBuffer = new unsigned char[nCapacity];
		setup(aNewBuffer,nCapacity,false,true);
		m_nLen = 0;
	}

	// --------------------------------------------------------------------------------------------------
	inline ByteBuffer::ByteBuffer(unsigned int nCapacity, unsigned int nMaxCapacity)  :
			m_nLen(0),
			m_nCapacity(0),
			m_nMaxCapacity(0),
			m_aData(NULL),
			m_bAutoDisposeFlag(false),
			m_nPos(0),
			m_nMarkedLen(0),
			m_nMarkedPos(0) {
		unsigned char* aNewBuffer = new unsigned char[nCapacity];
		setup(aNewBuffer,nCapacity,false,true);
		m_nLen = 0;
		m_nMaxCapacity = nMaxCapacity;
	}

	// --------------------------------------------------------------------------------------------------
	inline ByteBuffer::ByteBuffer(const ByteBuffer* poSrcBuffer) :
			m_nLen(0),
			m_nCapacity(0),
			m_nMaxCapacity(0),
			m_aData(NULL),
			m_bAutoDisposeFlag(false),
			m_nPos(0),
			m_nMarkedLen(0),
			m_nMarkedPos(0)  {
		setup ( poSrcBuffer->getData(), poSrcBuffer->getCapacity(), true, true );
		m_nMaxCapacity = poSrcBuffer->getMaxCapacity();
	}

	// --------------------------------------------------------------------------------------------------
	inline ByteBuffer::ByteBuffer(const ByteBuffer& poSrcBuffer) :
			m_nLen(0),
			m_nCapacity(0),
			m_nMaxCapacity(0),
			m_aData(NULL),
			m_bAutoDisposeFlag(false),
			m_nPos(0),
			m_nMarkedLen(0),
			m_nMarkedPos(0)  {
		setup ( poSrcBuffer.getData(), poSrcBuffer.getCapacity(), true, true );
		m_nMaxCapacity = poSrcBuffer.getMaxCapacity();
	}

	// --------------------------------------------------------------------------------------------------
	inline ByteBuffer::ByteBuffer(const unsigned char* poSrcBuffer, unsigned int nSrcBufLen, bool bCopingMode, bool bAutoDispose) :
			m_nLen(0),
			m_nCapacity(0),
			m_nMaxCapacity(0),
			m_aData(NULL),
			m_bAutoDisposeFlag(false),
			m_nPos(0),
			m_nMarkedLen(0),
			m_nMarkedPos(0)  {
		setup ( const_cast<unsigned char*>(poSrcBuffer), nSrcBufLen, bCopingMode, bAutoDispose );
	}


	// --------------------------------------------------------------------------------------------------
	inline ByteBuffer::~ByteBuffer() {
		disposeCurrent();
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::append (const unsigned char* aBuffer, unsigned int nBufLen ) {
		if(nBufLen + m_nLen > m_nCapacity)
			setCapacity(nBufLen + m_nLen);
		memcpy(m_aData + m_nLen, aBuffer, nBufLen);
		m_nLen+=nBufLen;
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::append (const ByteBuffer* poBuffer ) {
		append ( poBuffer->getData(), poBuffer->getLen() );
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::append (const ByteBuffer& oBuffer ) {
		append ( &oBuffer );
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::append ( const unsigned char nByte ) {
		if(m_nLen + 1 > m_nCapacity)
			setCapacity(m_nLen + 1);
		m_aData[m_nLen++] = nByte;
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::replace ( const unsigned char* aBuffer, const unsigned int nBufLen ) {
		setLen( 0 );
		append ( aBuffer, nBufLen );
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::replace ( const ByteBuffer& oBuffer ) {
		replace ( &oBuffer );
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::replace ( const ByteBuffer* oBuffer ) {
		replace ( oBuffer->getData(), oBuffer->getLen() );
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::cut ( unsigned int nBeginPos, unsigned int nEndPos, ByteBuffer* oBuffer ) {
		if(nBeginPos > getLen())
			nBeginPos = getLen();
		else
			if (nBeginPos < 0 )
				nBeginPos = 0;

		if(nEndPos > getLen() )
			nEndPos = getLen();
		else
			if (nEndPos < 0 )
				nEndPos = 0;

		unsigned int nSliceLen = (nEndPos-nBeginPos);
		if ( oBuffer!=NULL ) {
			oBuffer->append(getData()+nBeginPos,nSliceLen);
		}

		if (nEndPos !=getLen()) {
			memcpy ( m_aData + nBeginPos, m_aData + nEndPos, getLen() - nEndPos);
			setLen ( getLen() - nSliceLen );
		}
		else
			setLen(nBeginPos);
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::cut ( unsigned int nBeginPos, unsigned int nEndPos, ByteBuffer& oBuffer ) {
		cut ( nBeginPos, nEndPos, &oBuffer );
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::cut ( unsigned int nBeginPos, unsigned int nEndPos) {
		cut ( nBeginPos, nEndPos, NULL);
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::cut ( unsigned int nBeginPos, ByteBuffer* oBuffer ) {
		cut ( nBeginPos, getLen(), oBuffer );
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::cut ( unsigned int nBeginPos, ByteBuffer& oBuffer ) {
		cut ( nBeginPos, &oBuffer );
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::cut ( unsigned int nBeginPos ) {
		cut ( nBeginPos, (ByteBuffer*)NULL );
	}


	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::fill ( int nValue ) {
		memset(m_aData, nValue, m_nCapacity);
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::fillToLen ( int nValue ) {
		memset(m_aData, nValue, m_nLen);
	}


	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::mark() {
		m_nMarkedLen = m_nLen;
		m_nMarkedPos = m_nPos;
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::resetToMarker() {
		m_nLen = m_nMarkedLen;
		m_nPos = m_nMarkedPos;
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::clear() {
		m_nLen = 0;
		m_nPos = 0;
		m_nMarkedPos = 0;
		m_nMarkedLen = 0;
	}

	// --------------------------------------------------------------------------------------------------
	inline void ByteBuffer::dump() const {
		std::cout << "Buffer instance #" <<this << ". Size: " << getLen() << ". Capacity: "<< getCapacity() << ". Pos: " << getPos() <<std::endl;
		std::cout << " [ ";
		for(unsigned int i=0;i<getLen();i++) {
			unsigned int bt = (unsigned int)getData()[i];
			std::cout << (bt <= 0xF ? "0":"") << std::hex << bt << std::dec << " ";
		}
		std::cout << "]" <<std::endl;
	}

